package twilight.companion.entity.ai;

import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.Vec3;
import twilight.companion.entity.MiniYetiEntity;
import twilightforest.init.TFEntities;

import java.util.EnumSet;

public class MiniYetiRampageGoal extends Goal {
    private final MiniYetiEntity yeti;
    private int currentTimeOut;
    private int currentDuration;
    private final int maxTantrumTimeOut;
    private final int tantrumDuration;
    private int cooldownTime = 0;

    public MiniYetiRampageGoal(MiniYetiEntity yeti, int timeout, int duration) {
        this.yeti = yeti;
        this.currentTimeOut = timeout;
        this.maxTantrumTimeOut = timeout;
        this.tantrumDuration = duration;
        this.setFlags(EnumSet.of(Flag.MOVE, Flag.JUMP));
    }

    @Override
    public boolean canUse() {
        if (this.cooldownTime > 0) {
            this.cooldownTime--;
            return false;
        }
        
        if (this.yeti.getTarget() != null && this.yeti.canRampage()) {
            this.currentTimeOut--;
        }
        return this.currentTimeOut <= 0;
    }

    @Override
    public void start() {
        this.currentDuration = this.tantrumDuration;
        this.yeti.setRampaging(true);
        this.yeti.gameEvent(GameEvent.ENTITY_ROAR);
    }

    @Override
    public boolean canContinueToUse() {
        return currentDuration > 0;
    }

    @Override
    public void tick() {
        this.currentDuration--;

        if (this.yeti.getTarget() != null) {
            this.yeti.getLookControl().setLookAt(this.yeti.getTarget(), 10.0F, this.yeti.getMaxHeadXRot());
        }

        if (this.yeti.onGround()) {
            this.yeti.setDeltaMovement(0, 0.3D, 0);
            this.yeti.gameEvent(GameEvent.HIT_GROUND);
        }

        this.yeti.destroyBlocksInAABB(this.yeti.getBoundingBox().inflate(0.5, 1, 0.5).move(0, 1, 0));

        if (this.currentDuration % 10 == 0) {
            this.yeti.makeRandomBlockFall(15, 40);
        }

        if (this.currentDuration % 20 == 0) {
            this.yeti.makeBlockAboveTargetFall();
        }

        if (this.currentDuration < 30 && this.currentDuration % 8 == 0) {
            this.yeti.makeRandomBlockFall(8, 25);
        }

        if (this.currentDuration % 20 == 0) {
            twilight.companion.entity.projectile.MiniYetiIceBomb ice = 
            new twilight.companion.entity.projectile.MiniYetiIceBomb(TFEntities.THROWN_ICE.get(), this.yeti.level(), this.yeti);
            Vec3 vec = new Vec3(0.4F + this.yeti.getRandom().nextFloat() * 0.4F,
            0.4F + this.yeti.getRandom().nextFloat() * 0.2F, 0).yRot(this.yeti.getRandom().nextFloat() * 360F);
            ice.shoot(vec.x(), vec.y(), vec.z(), 0.3F + yeti.getRandom().nextFloat() * 0.2F, 0);
            this.yeti.gameEvent(GameEvent.PROJECTILE_SHOOT);
            this.yeti.level().addFreshEntity(ice);
        }
    }

    @Override
    public void stop() {
        this.currentTimeOut = this.maxTantrumTimeOut;
        this.cooldownTime = 200;
        this.yeti.setRampaging(false);
    }
}